{include file="public/header" /}
<blockquote class="layui-elem-quote">
    添加
</blockquote>
<style>
    .layui-upload-list {
        height: 100px;
        border: 1px solid #ccc;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    #ID-upload-demo-img {
        max-width: 100%;
        max-height: 100%;
    }
</style>
<form class="layui-form layui-form-pane kllxs-form" method="post">
    <div class="layui-form-item">
        <label class="layui-form-label">类别</label>
        <div class="layui-input-inline">
            <select name="category" id="">
                <option value="无类别">无类别</option>
                <option value="类别1">类别1</option>
                <option value="类别2">类别2</option>
                <option value="类别3">类别3</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上传</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="ID-upload-demo-btn">
                <i class="layui-icon layui-icon-upload"></i> 图片上传
            </button>
            <div style="width: 132px;">
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="ID-upload-demo-img">
                    <div id="ID-upload-demo-text"></div>
                </div>
                <div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="filter-demo">
                    <div class="layui-progress-bar" lay-percent=""></div>
                </div>
            </div>
        </div>
    </div>
</form>
{include file="public/footer" /}
<script>
    layui.use(['upload', 'jquery', 'element', 'layer'], function () {
        var upload = layui.upload;
        var $ = layui.$;
        var element = layui.element;
        var layer = layui.layer;

        // 单图片上传
        var uploadInst = upload.render({
            elem: '#ID-upload-demo-btn',
            data: {
                category: function () {
                    return $('select[name="category"]').val()
                }
            },
            url: '{:route("admin.index.upload")}',
            before: function (obj) {
                // 预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
                });

                element.progress('filter-demo', '0%'); // 进度条复位
                layer.msg('上传中', { icon: 16, time: 0 });
            },
            done: function (res) {
                if (res.code == 1) {
                    layer.msg(res.msg, { icon: res.code, time: 1000 }, function () {
                        var topDom = $(window.top.document).find(`.layui-tab-content .layui-show iframe`)[0].contentWindow;
                        topDom.layui.table.reload("table")
                        var index = parent.layer.getFrameIndex(window.name); // 先得到当前 iframe 层的索引
                        parent.layer.close(index); // 再执行关闭
                    })
                } else {
                    layer.msg(res.msg, { icon: res.code })
                }
                $('#ID-upload-demo-text').html(''); // 置空上传失败的状态
            },
            error: function () {
                // 演示失败状态，并实现重传
                var demoText = $('#ID-upload-demo-text');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            },
            // 进度条
            progress: function (n, elem, e) {
                element.progress('filter-demo', n + '%'); // 可配合 layui 进度条元素使用
                if (n == 100) {
                    layer.msg('上传完毕', { icon: 1 });
                }
            }
        });
    })
</script>